/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const { ProvidePlugin } = require("webpack");
module.exports = class OpenharmonyPolyfillPlugin {
  /**
   * 注入全局变量、全局模块
   */
  apply(compiler) {
    compiler.options.plugins.push(
      new ProvidePlugin({
        // primordials: require.resolve(
        //   "@ohos/openharmony-node-polyfill/lib/node/primordials.js"
        // ),
        Buffer: [
          require.resolve("../rollup-plugin-polyfill/polyfills/lib/buffer"),
          "Buffer",
        ],
        process: require.resolve(
          "../rollup-plugin-polyfill/polyfills/lib/process"
        ),
        global: require.resolve(
          "../rollup-plugin-polyfill/polyfills/lib/global"
        ),

        // escape: [
        //   require.resolve(
        //     "@ohos/openharmony-node-polyfill/lib/node/querystring"
        //   ),
        //   "escape",
        // ],
        // unescape: [
        //   require.resolve(
        //     "@ohos/openharmony-node-polyfill/lib/node/querystring"
        //   ),
        //   "unescape",
        // ],
        // setImmediate: [
        //   require.resolve("@ohos/openharmony-node-polyfill/lib/node/timers"),
        //   "setImmediate",
        // ],
      })
    );

    compiler.options.resolve.fallback = {
      events: require.resolve("../rollup-plugin-polyfill/polyfills/lib/events"),
      path: require.resolve("../rollup-plugin-polyfill/polyfills/lib/path"),
      buffer: require.resolve("../rollup-plugin-polyfill/polyfills/lib/buffer"),
      process: require.resolve(
        "../rollup-plugin-polyfill/polyfills/lib/process"
      ),
      stream: require.resolve(
        "../rollup-plugin-polyfill/polyfills/lib/stream/lib/ours"
      ),
      querystring: require.resolve(
        "../rollup-plugin-polyfill/polyfills/lib/querystring"
      ),
      util: require.resolve("../rollup-plugin-polyfill/polyfills/lib/util"),
      url: require.resolve("../rollup-plugin-polyfill/polyfills/lib/url"),
      timers: require.resolve("../rollup-plugin-polyfill/polyfills/lib/timers"),
      crypto: require.resolve("../rollup-plugin-polyfill/polyfills/lib/crypto"),
    };
  }
};
